'''
Created on Nov 26, 2009

@author: anch
'''
import unittest
import gamemodel.SubNeighborhood5x5 as mNB
import resource_manager
import interface.ConsoleAgent

class Test(unittest.TestCase):


	def testConsturtFromState(self):
		#preparation
		state = resource_manager.ResourceManager.constructStateMap("adam.txt")
#		interface.ConsoleAgent.ConsoleDescriptor.drawState(state)
		player = state.getCurrentPlayer()
						
		#Test return amount
		#should only return sub-case one
		#For the following method, please have a look at its doc_string
		sub_neighbors = mNB.Neighbor55.constructFromState(state, 0, 0, player)
		self.assertEqual(sub_neighbors.count(None), 2)
		self.assertFalse(sub_neighbors[0] is None)
		
		#should - return all sub-case one, two, three
		sub_neighbors = mNB.Neighbor55.constructFromState(state, 4, 0, player)
		self.assertEqual(sub_neighbors.count(None), 0)
		self.assertFalse(sub_neighbors[2] is None)
		
		#should only return sub-case three
		sub_neighbors = mNB.Neighbor55.constructFromState(state, 8, 0, player)
		self.assertEqual(sub_neighbors.count(None), 2)
		pass
	
	def testGetProbability(self):
		state = resource_manager.ResourceManager.constructStateMap("adam.txt")
		player = state.getCurrentPlayer()
		
		sub_neighbor = mNB.Neighbor55.constructFromState(state, 0, 0, player)[0]
		
		#please have a look at the doc_string of getMoveProb(move)
		prob = sub_neighbor.getMoveProb(0)
		self.assertTrue(prob >= 0 )
		self.assertTrue(prob < 100 )
		
		#cannot get probability for Move D for sub case one
		self.assertRaises(RuntimeError, sub_neighbor.getMoveProb, 3)
		
		#but ok for sub case two
		sub_neighbor = mNB.Neighbor55.constructFromState(state, 4, 0, player)[1]
		prob = sub_neighbor.getMoveProb(3)
		self.assertTrue(prob >= 0 )
		self.assertTrue(prob < 100 )
	
	def testParameterFormForConstructSkeleton(self):
		#Test various parameter type
		#Test whether the different parameter forms(in same meaning) -
		# return same result
		instance1= mNB.Neighbor55.construct_skeleton(0)
		instance2 = mNB.Neighbor55.construct_skeleton((0,0))
		self.assertEqual(instance1, instance2)
		
	def testGetByCoord(self):
		#Test retrieve focused piece
		instance = mNB.Neighbor55.construct_skeleton(0)
		instance.setByCoord(0, 2, 1)
		
		self.assertEqual(instance.getByCoord(0, 0), 1)
	
	def testEncode(self):
		state = resource_manager.ResourceManager.constructStateMap("abraham.txt")
#		interface.ConsoleAgent.ConsoleDescriptor.drawState(state)
		player = state.getCurrentPlayer()
						
		sub_neighbors = mNB.Neighbor55.constructFromState(state, 4, 0, player)
#		sub_neighbors[1].draw()
		self.assertEqual(sub_neighbors[0].encode(), 'YUX\x00')
		self.assertEqual(sub_neighbors[1].encode(), '\x92TF\x01')
		self.assertEqual(sub_neighbors[2].encode(), 'aTP\x00')
		pass


if __name__ == "__main__":
	#import sys;sys.argv = ['', 'Test.testName']
	unittest.main()